<!DOCTYPE html>
<html lang="zh-CN">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="robots" content="noodp" />
        <title>多级折叠导航栏 - 欢迎来到SY的博客</title><meta name="description" content="多级折叠导航栏"><meta property="og:title" content="多级折叠导航栏" />
<meta property="og:description" content="多级折叠导航栏" />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://acking-you.gitee.io/posts/%C3%A5%C3%A7%C2%BA%C3%A6%C3%A5-%C3%A5%C3%A8%C2%AA%C3%A6-/" /><meta property="og:image" content="https://acking-you.gitee.io/logo.png"/><meta property="article:section" content="posts" />
<meta property="article:published_time" content="2022-09-04T00:00:00+00:00" />
<meta property="article:modified_time" content="2022-09-04T00:00:00+00:00" />

<meta name="twitter:card" content="summary_large_image"/>
<meta name="twitter:image" content="https://acking-you.gitee.io/logo.png"/>

<meta name="twitter:title" content="多级折叠导航栏"/>
<meta name="twitter:description" content="多级折叠导航栏"/>
<meta name="application-name" content="FeelIt">
<meta name="apple-mobile-web-app-title" content="FeelIt"><meta name="theme-color" content="#ffffff"><meta name="msapplication-TileColor" content="#da532c"><link rel="canonical" href="https://acking-you.gitee.io/posts/%C3%A5%C3%A7%C2%BA%C3%A6%C3%A5-%C3%A5%C3%A8%C2%AA%C3%A6-/" /><link rel="prev" href="https://acking-you.gitee.io/posts/%C3%A6%C3%A8%C3%A4%C2%AA%C3%A8%C3%A7/" /><link rel="stylesheet" href="/css/page.min.css"><link rel="stylesheet" href="/css/home.min.css"><script type="application/ld+json">
    {
        "@context": "http://schema.org",
        "@type": "BlogPosting",
        "headline": "多级折叠导航栏",
        "inLanguage": "zh-CN",
        "mainEntityOfPage": {
            "@type": "WebPage",
            "@id": "https:\/\/acking-you.gitee.io\/posts\/%C3%A5%C3%A7%C2%BA%C3%A6%C3%A5-%C3%A5%C3%A8%C2%AA%C3%A6-\/"
        },"genre": "posts","keywords": "多级折叠导航栏","wordcount":  647 ,
        "url": "https:\/\/acking-you.gitee.io\/posts\/%C3%A5%C3%A7%C2%BA%C3%A6%C3%A5-%C3%A5%C3%A8%C2%AA%C3%A6-\/","datePublished": "2022-09-04T00:00:00+00:00","dateModified": "2022-09-04T00:00:00+00:00","publisher": {
            "@type": "Organization",
            "name": "作者"},"author": {
                "@type": "Person",
                "name": "作者"
            },"description": "多级折叠导航栏"
    }
    </script></head><body data-header-desktop="auto" data-header-mobile="auto"><script>(window.localStorage && localStorage.getItem('theme') ? localStorage.getItem('theme') === 'dark' : ('auto' === 'auto' ? window.matchMedia('(prefers-color-scheme: dark)').matches : 'auto' === 'dark')) && document.body.setAttribute('theme', 'dark');</script>

        <div id="mask"></div><div class="wrapper"><header class="desktop" id="header-desktop">
    <div class="header-wrapper">
        <div class="header-title">
            <a href="/" title="欢迎来到SY的博客">欢迎来到SY的博客</a>
        </div>
        <div class="menu">
            <div class="menu-inner"><a class="menu-item" href="/posts/"> 文章 </a><a class="menu-item" href="/tags/"> 标签 </a><a class="menu-item" href="/categories/"> 分类 </a><span class="menu-item delimiter"></span><span class="menu-item search" id="search-desktop">
                        <input type="text" placeholder="搜索文章标题或内容..." id="search-input-desktop">
                        <a href="#" class="search-button search-toggle" id="search-toggle-desktop" title="搜索">
                            <i class="fas fa-search fa-fw"></i>
                        </a>
                        <a href="#" class="search-button search-clear" id="search-clear-desktop" title="清空">
                            <i class="fas fa-times-circle fa-fw"></i>
                        </a>
                        <span class="search-button search-loading" id="search-loading-desktop">
                            <i class="fas fa-spinner fa-fw fa-spin"></i>
                        </span>
                    </span><a href="javascript:void(0);" class="menu-item theme-switch" title="切换主题">
                    <i class="fas fa-adjust fa-fw"></i>
                </a>
            </div>
        </div>
    </div>
</header><header class="mobile" id="header-mobile">
    <div class="header-container">
        <div class="header-wrapper">
            <div class="header-title">
                <a href="/" title="欢迎来到SY的博客">欢迎来到SY的博客</a>
            </div>
            <div class="menu-toggle" id="menu-toggle-mobile">
                <span></span><span></span><span></span>
            </div>
        </div>
        <div class="menu" id="menu-mobile"><div class="search-wrapper">
                    <div class="search mobile" id="search-mobile">
                        <input type="text" placeholder="搜索文章标题或内容..." id="search-input-mobile">
                        <a href="#" class="search-button search-toggle" id="search-toggle-mobile" title="搜索">
                            <i class="fas fa-search fa-fw"></i>
                        </a>
                        <a href="#" class="search-button search-clear" id="search-clear-mobile" title="清空">
                            <i class="fas fa-times-circle fa-fw"></i>
                        </a>
                        <span class="search-button search-loading" id="search-loading-mobile">
                            <i class="fas fa-spinner fa-fw fa-spin"></i>
                        </span>
                    </div>
                    <a href="#" class="search-cancel" id="search-cancel-mobile">
                        取消
                    </a>
                </div><a class="menu-item" href="/posts/" title="">文章</a><a class="menu-item" href="/tags/" title="">标签</a><a class="menu-item" href="/categories/" title="">分类</a><div class="menu-item"><a href="javascript:void(0);" class="theme-switch" title="切换主题">
                    <i class="fas fa-adjust fa-fw"></i>
                </a>
            </div></div>
    </div>
</header><div class="search-dropdown desktop">
    <div id="search-dropdown-desktop"></div>
</div>
<div class="search-dropdown mobile">
    <div id="search-dropdown-mobile"></div>
</div><main class="main">
                <div class="container"><div class="toc" id="toc-auto">
            <h2 class="toc-title">目录</h2>
            <div class="toc-content" id="toc-content-auto"></div>
        </div><article class="page single" data-toc="disable"><div class="featured-image"><img
        class="lazyload"
        src="/svg/loading.min.svg"
        data-src="https://w.wallhaven.cc/full/6q/wallhaven-6qw6x6.jpg"
        data-srcset="https://w.wallhaven.cc/full/6q/wallhaven-6qw6x6.jpg, https://w.wallhaven.cc/full/6q/wallhaven-6qw6x6.jpg 1.5x, https://w.wallhaven.cc/full/6q/wallhaven-6qw6x6.jpg 2x"
        data-sizes="auto"
        alt="https://w.wallhaven.cc/full/6q/wallhaven-6qw6x6.jpg"
        title="多级折叠导航栏" /></div><div class="single-card" data-image="true"><h2 class="single-title animated flipInX">多级折叠导航栏</h2><div class="post-meta">
                <div class="post-meta-line"><span class="post-author"><a href="/" title="Author" rel=" author" class="author"><i class="fas fa-user-circle fa-fw"></i>作者</a></span>&nbsp;<span class="post-category">出版于  <a href="/categories/qtwidget%E9%A1%B9%E7%9B%AE/"><i class="far fa-folder fa-fw"></i>QtWidget项目</a></span></div>
                <div class="post-meta-line"><span><i class="far fa-calendar-alt fa-fw"></i>&nbsp;<time datetime="2022-09-04">2022-09-04</time></span>&nbsp;<span><i class="fas fa-pencil-alt fa-fw"></i>&nbsp;约 647 字</span>&nbsp;
                    <span><i class="far fa-clock fa-fw"></i>&nbsp;预计阅读 2 分钟</span>&nbsp;</div>
            </div>
            
            <hr><div class="details toc" id="toc-static"  data-kept="">
                    <div class="details-summary toc-title">
                        <span>目录</span>
                        <span><i class="details-icon fas fa-angle-right"></i></span>
                    </div>
                    <div class="details-content toc-content" id="toc-content-static"><nav id="TableOfContents">
  <ul>
    <li><a href="#1多级折叠导航栏效果展示">1、多级折叠导航栏效果展示</a></li>
    <li><a href="#2效果特点">2、效果特点</a></li>
    <li><a href="#3实现思路">3、实现思路</a></li>
    <li><a href="#4使用该导航栏">4、使用该导航栏</a></li>
  </ul>
</nav></div>
                </div><div class="content" id="content"><h2 id="1多级折叠导航栏效果展示">1、多级折叠导航栏效果展示</h2>
<p><img
        class="lazyload"
        src="/svg/loading.min.svg"
        data-src="https://raw.githubusercontent.com/jjking20010901/typora/master/img202209041031369.png"
        data-srcset="https://raw.githubusercontent.com/jjking20010901/typora/master/img202209041031369.png, https://raw.githubusercontent.com/jjking20010901/typora/master/img202209041031369.png 1.5x, https://raw.githubusercontent.com/jjking20010901/typora/master/img202209041031369.png 2x"
        data-sizes="auto"
        alt="https://raw.githubusercontent.com/jjking20010901/typora/master/img202209041031369.png"
        title="image-20220904103157333" /></p>
<h2 id="2效果特点">2、效果特点</h2>
<ul>
<li>支持多级菜单显示</li>
<li>支持鼠标滚动效果</li>
<li>菜单可折叠</li>
<li>可嵌套在项目中</li>
</ul>
<h2 id="3实现思路">3、实现思路</h2>
<p>该导航栏是以QTreeWidget为实现部件，使用样式表进行美化，在ui界面中，点击treeWidget部件，然后设置列参数为1，然后添加列项目元素，在每一个根元素中添加一些字项目节点。</p>
<p>最后通过加载QResourceFile添加资源图片，给每个根元素添加图片。</p>
<p>下面是初始化treeWidget的代码：</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-CPP" data-lang="CPP"><span class="cm">/**
</span><span class="cm">@brief:设置QTreeWidget样式并初始化QTreeWidget
</span><span class="cm">@param:void
</span><span class="cm">@return:void
</span><span class="cm">*/</span>

<span class="kt">void</span> <span class="n">Widget</span><span class="o">::</span><span class="n">init</span><span class="p">()</span>
<span class="p">{</span>
    <span class="c1">//设置QTreeWidget样式
</span><span class="c1"></span>    <span class="k">this</span><span class="o">-&gt;</span><span class="n">ui</span><span class="o">-&gt;</span><span class="n">treeWidget</span><span class="o">-&gt;</span><span class="n">setStyleSheet</span><span class="p">(</span><span class="n">QString</span><span class="p">(</span><span class="s">&#34;QTreeWidget::item{height:60px;}&#34;</span>
                                                <span class="s">&#34;QTreeWidget{background-color:rgb(33,45,62);&#34;</span>
                                                <span class="s">&#34;font:20px;color:rgb(255,255,255);}&#34;</span>
                                                <span class="s">&#34;QTreeView::arrow{background-color:rgb(255,255,255);}&#34;</span><span class="p">));</span>
    <span class="c1">//隐藏QTreeWidget的Header
</span><span class="c1"></span>    <span class="k">this</span><span class="o">-&gt;</span><span class="n">ui</span><span class="o">-&gt;</span><span class="n">treeWidget</span><span class="o">-&gt;</span><span class="n">setHeaderHidden</span><span class="p">(</span><span class="nb">true</span><span class="p">);</span>
    <span class="c1">//根据行和列自动调整headView的大小
</span><span class="c1"></span>    <span class="n">QHeaderView</span> <span class="o">*</span><span class="n">header</span><span class="o">=</span><span class="k">this</span><span class="o">-&gt;</span><span class="n">ui</span><span class="o">-&gt;</span><span class="n">treeWidget</span><span class="o">-&gt;</span><span class="n">header</span><span class="p">();</span>
    <span class="n">header</span><span class="o">-&gt;</span><span class="n">setSectionResizeMode</span><span class="p">(</span><span class="n">QHeaderView</span><span class="o">::</span><span class="n">ResizeToContents</span><span class="p">);</span>

    <span class="c1">//设置QTreeWidget的icon大小
</span><span class="c1"></span>    <span class="k">this</span><span class="o">-&gt;</span><span class="n">ui</span><span class="o">-&gt;</span><span class="n">treeWidget</span><span class="o">-&gt;</span><span class="n">setIconSize</span><span class="p">(</span><span class="n">QSize</span><span class="p">(</span><span class="mi">32</span><span class="p">,</span><span class="mi">32</span><span class="p">));</span>

    <span class="c1">//隐藏QTreeWidget的水平和垂直滚动条
</span><span class="c1"></span>    <span class="k">this</span><span class="o">-&gt;</span><span class="n">ui</span><span class="o">-&gt;</span><span class="n">treeWidget</span><span class="o">-&gt;</span><span class="n">setHorizontalScrollBarPolicy</span><span class="p">(</span><span class="n">Qt</span><span class="o">::</span><span class="n">ScrollBarAlwaysOff</span><span class="p">);</span>
    <span class="k">this</span><span class="o">-&gt;</span><span class="n">ui</span><span class="o">-&gt;</span><span class="n">treeWidget</span><span class="o">-&gt;</span><span class="n">setVerticalScrollBarPolicy</span><span class="p">(</span><span class="n">Qt</span><span class="o">::</span><span class="n">ScrollBarAlwaysOff</span><span class="p">);</span>
    <span class="c1">//设置treeWidget展开和隐藏动画
</span><span class="c1"></span>    <span class="k">this</span><span class="o">-&gt;</span><span class="n">ui</span><span class="o">-&gt;</span><span class="n">treeWidget</span><span class="o">-&gt;</span><span class="n">setAnimated</span><span class="p">(</span><span class="nb">true</span><span class="p">);</span>

    <span class="c1">//默认展开treeWidget所有项
</span><span class="c1"></span>    <span class="k">this</span><span class="o">-&gt;</span><span class="n">ui</span><span class="o">-&gt;</span><span class="n">treeWidget</span><span class="o">-&gt;</span><span class="n">expandAll</span><span class="p">();</span>
    <span class="c1">//去除treeWidget的边框
</span><span class="c1"></span>    <span class="k">this</span><span class="o">-&gt;</span><span class="n">ui</span><span class="o">-&gt;</span><span class="n">treeWidget</span><span class="o">-&gt;</span><span class="n">setFrameStyle</span><span class="p">(</span><span class="n">QFrame</span><span class="o">::</span><span class="n">NoFrame</span><span class="p">);</span>
<span class="p">}</span>
</code></pre></div><h2 id="4使用该导航栏">4、使用该导航栏</h2>
<p>通过上面的代码和对ui界面的设计，我们就可以得到一个精美的导航栏，那么我们如何将它嵌套在我们的项目中呢？下面详细讲解一下：</p>
<p>首先创建一个带ui界面的Test类(测试类)，然后就是让他继承QWidget(非常关键)，在它的ui中拖入一个Widget控件，然后在其构造函数加入如下代码：</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-CPP" data-lang="CPP">	<span class="n">w</span><span class="o">=</span><span class="k">new</span> <span class="n">Widget</span><span class="p">();</span>
    <span class="n">QVBoxLayout</span> <span class="o">*</span><span class="n">layout</span><span class="o">=</span><span class="k">new</span> <span class="n">QVBoxLayout</span><span class="p">();</span>
    <span class="n">layout</span><span class="o">-&gt;</span><span class="n">addWidget</span><span class="p">(</span><span class="n">w</span><span class="p">);</span>
    <span class="n">layout</span><span class="o">-&gt;</span><span class="n">setContentsMargins</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">);</span>
    <span class="k">this</span><span class="o">-&gt;</span><span class="n">ui</span><span class="o">-&gt;</span><span class="n">left_widget</span><span class="o">-&gt;</span><span class="n">setLayout</span><span class="p">(</span><span class="n">layout</span><span class="p">);</span>
</code></pre></div><p>然后我们可以得到如下效果：</p>
<p><img
        class="lazyload"
        src="/svg/loading.min.svg"
        data-src="https://raw.githubusercontent.com/jjking20010901/typora/master/img202209041028364.png"
        data-srcset="https://raw.githubusercontent.com/jjking20010901/typora/master/img202209041028364.png, https://raw.githubusercontent.com/jjking20010901/typora/master/img202209041028364.png 1.5x, https://raw.githubusercontent.com/jjking20010901/typora/master/img202209041028364.png 2x"
        data-sizes="auto"
        alt="https://raw.githubusercontent.com/jjking20010901/typora/master/img202209041028364.png"
        title="image-20220904102813298" /></p>
<p>这样就嵌套在了Test中了，右边的Widget我没有美化，所以看起来不好看，自己可以自由设计右侧的内容。</p>
</div><div class="post-footer" id="post-footer">
    <div class="post-info"><div class="post-info-tag"><span><a href="/tags/%E5%A4%9A%E7%BA%A7%E6%8A%98%E5%8F%A0%E5%AF%BC%E8%88%AA%E6%A0%8F/">多级折叠导航栏</a>
                </span></div><div class="post-info-line"><div class="post-info-mod">
                <span>更新于 2022-09-04</span>
            </div><div class="post-info-mod"></div>
        </div><div class="post-info-share">
            <span><a href="javascript:void(0);" title="分享到 Twitter" data-sharer="twitter" data-url="https://acking-you.gitee.io/posts/%C3%A5%C3%A7%C2%BA%C3%A6%C3%A5-%C3%A5%C3%A8%C2%AA%C3%A6-/" data-title="多级折叠导航栏" data-hashtags="多级折叠导航栏"><i class="fab fa-twitter fa-fw"></i></a><a href="javascript:void(0);" title="分享到 Facebook" data-sharer="facebook" data-url="https://acking-you.gitee.io/posts/%C3%A5%C3%A7%C2%BA%C3%A6%C3%A5-%C3%A5%C3%A8%C2%AA%C3%A6-/" data-hashtag="多级折叠导航栏"><i class="fab fa-facebook-square fa-fw"></i></a><a href="javascript:void(0);" title="分享到 WhatsApp" data-sharer="whatsapp" data-url="https://acking-you.gitee.io/posts/%C3%A5%C3%A7%C2%BA%C3%A6%C3%A5-%C3%A5%C3%A8%C2%AA%C3%A6-/" data-title="多级折叠导航栏" data-web><i class="fab fa-whatsapp fa-fw"></i></a><a href="javascript:void(0);" title="分享到 Line" data-sharer="line" data-url="https://acking-you.gitee.io/posts/%C3%A5%C3%A7%C2%BA%C3%A6%C3%A5-%C3%A5%C3%A8%C2%AA%C3%A6-/" data-title="多级折叠导航栏"><i class="fab fa-line fa-fw"></i></a><a href="javascript:void(0);" title="分享到 微博" data-sharer="weibo" data-url="https://acking-you.gitee.io/posts/%C3%A5%C3%A7%C2%BA%C3%A6%C3%A5-%C3%A5%C3%A8%C2%AA%C3%A6-/" data-title="多级折叠导航栏" data-image="https://w.wallhaven.cc/full/6q/wallhaven-6qw6x6.jpg"><i class="fab fa-weibo fa-fw"></i></a><a href="javascript:void(0);" title="分享到 Myspace" data-sharer="myspace" data-url="https://acking-you.gitee.io/posts/%C3%A5%C3%A7%C2%BA%C3%A6%C3%A5-%C3%A5%C3%A8%C2%AA%C3%A6-/" data-title="多级折叠导航栏" data-description="多级折叠导航栏"><i data-svg-src="/lib/simple-icons/icons/myspace.min.svg"></i></a><a href="javascript:void(0);" title="分享到 Blogger" data-sharer="blogger" data-url="https://acking-you.gitee.io/posts/%C3%A5%C3%A7%C2%BA%C3%A6%C3%A5-%C3%A5%C3%A8%C2%AA%C3%A6-/" data-title="多级折叠导航栏" data-description="多级折叠导航栏"><i class="fab fa-blogger fa-fw"></i></a><a href="javascript:void(0);" title="分享到 Evernote" data-sharer="evernote" data-url="https://acking-you.gitee.io/posts/%C3%A5%C3%A7%C2%BA%C3%A6%C3%A5-%C3%A5%C3%A8%C2%AA%C3%A6-/" data-title="多级折叠导航栏"><i class="fab fa-evernote fa-fw"></i></a></span>
        </div></div><div class="post-nav"><a href="/posts/%C3%A6%C3%A8%C3%A4%C2%AA%C3%A8%C3%A7/" class="prev" rel="prev" title="汽车仪表盘"><i class="fas fa-angle-left fa-fw"></i>Previous Post</a></div></div>
</div></article></div>
            </main>
            <footer class="footer"><div class="footer-container"><div class="footer-line">由 <a href="https://gohugo.io/" target="_blank" rel="noopener noreffer" title="Hugo 0.86.0">Hugo</a> 强力驱动 | 主题 - <a href="https://github.com/khusika/FeelIt" target="_blank" rel="noopener noreffer" title="FeelIt 1.0.1"><i class="fas fa-hand-holding-heart fa-fw"></i> FeelIt</a>
        </div><div class="footer-line" itemscope itemtype="http://schema.org/CreativeWork"><i class="far fa-copyright fa-fw"></i><span itemprop="copyrightYear">2022</span><span class="author" itemprop="copyrightHolder">&nbsp;<a href="/"></a></span></div>
</div>
</footer>
        </div>

        <div id="fixed-buttons"><a href="#" id="back-to-top" class="fixed-button" title="回到顶部">
                <i class="fas fa-chevron-up fa-fw"></i>
            </a></div><link rel="stylesheet" href="/lib/fontawesome-free/all.min.css"><link rel="stylesheet" href="/lib/animate/animate.min.css"><link rel="stylesheet" href="/lib/katex/katex.min.css"><link rel="stylesheet" href="/lib/katex/copy-tex.min.css"><script src="/lib/autocomplete/autocomplete.min.js"></script><script src="/lib/lunr/lunr.min.js"></script><script src="/lib/lunr/lunr.stemmer.support.min.js"></script><script src="/lib/lunr/lunr.zh.min.js"></script><script src="/lib/lazysizes/lazysizes.min.js"></script><script src="/lib/clipboard/clipboard.min.js"></script><script src="/lib/sharer/sharer.min.js"></script><script src="/lib/katex/katex.min.js"></script><script src="/lib/katex/auto-render.min.js"></script><script src="/lib/katex/copy-tex.min.js"></script><script src="/lib/katex/mhchem.min.js"></script><script>window.config={"code":{"copyTitle":"复制到剪贴板","maxShownLines":200},"comment":{},"math":{"delimiters":[{"display":true,"left":"$$","right":"$$"},{"display":true,"left":"\\[","right":"\\]"},{"display":false,"left":"$","right":"$"},{"display":false,"left":"\\(","right":"\\)"}],"strict":false},"search":{"highlightTag":"em","lunrIndexURL":"/index.json","lunrLanguageCode":"zh","lunrSegmentitURL":"/lib/lunr/lunr.segmentit.js","maxResultLength":100,"noResultsFound":"没有找到结果","snippetLength":50,"type":"lunr"}};</script><script src="/js/theme.min.js"></script></body></html>
